package org.spring.springboot.app.community.notice.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.community.notice.domain.po.CommunityNoticePO;
import org.spring.springboot.app.community.notice.domain.vo.CommunityNoticeVO;
import org.spring.springboot.app.community.notice.domain.vo.CommunityNoticesaveVO;
import org.spring.springboot.app.community.notice.domain.vo.CommunityNoticeupdateVO;
import org.spring.springboot.app.community.notice.domain.vo.NoticeSearchVO;
import org.spring.springboot.app.community.notice.service.ICommunityNoticeService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(description="社区-公告操作接口",tags = ApiIndex.CommunityNoticeController)
@RequestMapping(value = "/api/communityNotice")
@RestController
public class CommunityNoticeController {

    @Autowired
    private ICommunityNoticeService communityNoticeService;

    @ApiOperation(value = "查询楼盘公告列表-分页")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<CommunityNoticeVO>>> findPage(
            @ApiIgnore User user,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String title,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        NoticeSearchVO nsearch=new NoticeSearchVO();
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        nsearch.setStartCreateDate(startCreateDate);
        nsearch.setEndCreateDate(endCreateDate);
        nsearch.setTitle(title);

        R<PageInfo<List<CommunityNoticeVO>>> result = new R<PageInfo<List<CommunityNoticeVO>>>();
        List<CommunityNoticeVO> _list = communityNoticeService.findPage(nsearch,pageNum, pageSize,user.getOfficeId());
        PageInfo p =new PageInfo(_list);
        p.setList(getSysDictVOS(p.getList()));
        result.setData(p);
        return result;
    }

    private List<CommunityNoticeVO> getSysDictVOS(List<CommunityNoticeVO> _list) {
        List<CommunityNoticeVO> list = new ArrayList<CommunityNoticeVO>();
        if (_list != null && _list.size()>0) {
            for (CommunityNoticeVO po : _list) {
                CommunityNoticeVO vo = new CommunityNoticeVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<CommunityNoticePO> find(
            @ApiParam(value = "楼盘公告id", required = true) @PathVariable("id") String id
    ) {
        R<CommunityNoticePO> result = new R<CommunityNoticePO>();
        CommunityNoticePO po = communityNoticeService.find(id);
        CommunityNoticePO vo = new CommunityNoticePO();
        BeanUtils.copyProperties(po,vo);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "保存楼盘公告信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiIgnore User user,
            @ApiParam(value = "字典对象", required = true) @RequestBody CommunityNoticesaveVO communitysaveNoticeVO
    ) {
        R result = new R();
        communitysaveNoticeVO.setOfficeId(user.getOfficeId());
        CommunityNoticePO po = new CommunityNoticePO();
        BeanUtils.copyProperties(communitysaveNoticeVO,po);
        int code = communityNoticeService.save(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("保存失败");
        }
        return result;
    }


    @ApiOperation(value = "修改楼盘公告信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiIgnore User user,
            @ApiParam(value = "楼盘公告对象", required = true) @RequestBody CommunityNoticeupdateVO sysCnoticeVO
    ) {
        R result = new R();
        sysCnoticeVO.setOfficeId(user.getOfficeId());
        CommunityNoticePO po = new CommunityNoticePO();
        BeanUtils.copyProperties(sysCnoticeVO,po);
        int code = communityNoticeService.update(po);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("修改失败");
        }
        return result;
    }

    @ApiOperation(value = "删除楼盘公告信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "楼盘公告id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = communityNoticeService.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     *  批量删除
     * @param ids
     */
    @ApiOperation(value="批量删除公告信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public R deletes(@ApiParam(value = "公告ids(用,分割)", required = true) @RequestParam("ids") String ids) {
        R result = new R();
        int code = communityNoticeService.deletes(ids);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }
}
